package com.boilerCloud.dao.repair;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.boilerCloud.dao.BaseDao;
import com.boilerCloud.dto.HzlqswPage;
import com.boilerCloud.dto.repair.RepairPlan;
import com.boilerCloud.util.StringUtil;

@Repository
public class RepairPlanDao extends BaseDao {

	public void qryList(RepairPlan model, HzlqswPage<RepairPlan> page)throws Exception {
		String countsql = "select count(*) from repair_plan a "
				+ " left join d_device_info b on b.id = a.device_id"
				+ " where a.is_delete='n' ";
			
		String listSql = "select a.*,b.name deviceName,e.name customerName,c.id workId "
					+ " from repair_plan a "
					+ " left join d_device_info b on b.id = a.device_id"
					+ " left join app_user e on b.user_id = e.id" 
					+ " left join work_order c on c.repair_id = a.id" 
					+ " where a.is_delete='n' ";
		List<Object> params= new ArrayList<Object>();
		if(!StringUtil.isEmpty(model.getRepairNo())){
			countsql += " and a.repair_no like ?";
			listSql += " and a.repair_no like ?";
			params.add("%" + model.getRepairNo() +"%");
		}
		
		
		if(!StringUtil.isEmpty(model.getDeviceName())){
			countsql += " and b.name like ?";
			listSql += " and b.name like ?";
			params.add("%" + model.getDeviceName() +"%");
		}
	
		listSql += " order by id desc ";
		this.queryListByPage(countsql, params, listSql, params, page, RepairPlan.class);
		
	}

	public void save(RepairPlan model) throws Exception {
		String sql = "insert into repair_plan(device_id,description,contact_name,contact_phone,"
				+ "status,create_time,modify_time,is_delete,repair_no) values (?,?,?,?,1,now(),now(),'n',?)";
		List<Object> param = new ArrayList<>();
		param.add(model.getDeviceId());
		param.add(model.getDescription());
		param.add(model.getContactName());
		param.add(model.getContactPhone());
		param.add(model.getRepairNo());
		this.jdbcTemplate.update(sql,param.toArray());
	}

	public RepairPlan getById(int id) throws Exception{
		String sql = "select a.*,b.name deviceName from repair_plan a "
				+ " left join d_device_info b on b.id = a.device_id where a.id =? limit 1";
		return this.queryForObject(sql, RepairPlan.class,id);
		
	}

	public void updateStatus(int id, int status) {
		String sql = "update repair_plan set status=? where id=?";
		this.jdbcTemplate.update(sql,status,id);
		
	}

}
